package com.dhecp.project.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dhecp.project.business.domain.LogisticsMmodeGrads;
import com.dhecp.project.logstics.mapper.LogsticsMmodeGradsMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.layui.GridColumnsPropertyModel;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.system.domain.SysDepotInfo;
import com.dhecp.project.system.service.SysDepotInfoService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 仓库信息 控制层
 * @author DH-lwl
 * @version 2019年11月28日
 */
@Controller
@RequestMapping("/system/depot")
public class SysDepotInfoController extends BaseController{

	private String prefix = "system/depot";
    private String moduleTable = ModuleTable.DEPOT_INFO;
    
    @Autowired
    private SysDepotInfoService depotInfoService;
    
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;
    
    @Autowired
    private GridColumnsPropertyModel gridColumnsPropertyModel;

    @Autowired
    private LogsticsMmodeGradsMapper logsticsMmodeGradsMapper;
    
    /**
     * 打开仓库列表
     * @param
     * @return ModelAndView
     * @author lwl
     */
	@RequiresPermissions("system:depot:view")
    @GetMapping()
    public ModelAndView dataList(@RequestParam Map<String, Object> params)
    {
		params.put("moduleTable", moduleTable);
    	params.put("entity", new SysDepotInfo()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	Subject userSubject = ShiroUtils.getSubject();
    	Integer toolBarWidth = 0;
    	if (userSubject.isPermitted("system:depot:delete")){	//当前登录用户主体是否有删除的权限
        	params.put("checkBox", "yes"); //生成表格gridColumns时包含复选
        	toolBarWidth = toolBarWidth + 85;
    	}
    	if (userSubject.isPermitted("system:depot:edit")){ //当前登录用户主体是否有修改的权限
    		toolBarWidth = toolBarWidth + 85;
    	}
    	params.put("toolBarWidth", toolBarWidth);
    	
		ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
        try{
            dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
            if (dictionaryForFieldMap.isEmpty()){
                ajaxResult = AjaxResult.error("未获取到字段属性。");
            }else{
                ajaxResult = AjaxResult.success("数据获取成功。");
            }
        }
        catch(Exception e){
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
        mv.addObject("ajaxResult", ajaxResult);
        mv.addObject("gridColumns", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));
        mv.addObject("moduleTable", moduleTable);
        mv.setViewName(prefix + "/depot");
		return mv;
    }
	
	/**
     * 查询所有仓库信息
     * @param params
     * @return TableDataInfo
     * @author lwl
     */
	@GetMapping("/list")
//    @RequiresPermissions("system:depot:list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params)
    {
		params.put("moduleTable", moduleTable);
        List<SysDepotInfo> depotlist = new ArrayList<SysDepotInfo>();
        try {
        	depotlist = depotInfoService.queryDepotInfo(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(depotlist);
    }
	
	/**
     * 打开仓库选择页面
     * @param params
     * @return ModelAndView
     * @author lwl
     */
    @GetMapping("/select")
    public ModelAndView selectDepotList(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTable);
    	params.put("entity", new SysDepotInfo()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	params.put("toolBarWidth", 0);
    	if(params.get("pagetype") !=null && params.get("pagetype").toString().equalsIgnoreCase("checkbox"))params.put("checkBox", "yes");
        else params.put("radio", "yes");
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        mv.addObject("ajaxResult", ajaxResult);
        mv.addObject("gridColumns", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));
        mv.addObject("prefix", prefix);
        mv.addObject("moduleTable", moduleTable);
        mv.setViewName("common/select");
        return mv;
    }
	
	/**
     * 打开新增仓库信息页面
     * @param params
     * @return ModelAndView
     * @author lwl
     */
    @RequiresPermissions("system:depot:add")
    @GetMapping("/add")
    public ModelAndView depotAdd(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTable);
    	params.put("entity", new SysDepotInfo()); //用于获取实体属性名取代对应字典字段名，解决大小写敏感问题
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>(); 
        try{
            dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
            if (dictionaryForFieldMap.isEmpty()){
                ajaxResult = AjaxResult.error("未获取到字段属性。");
            }else{
                ajaxResult = AjaxResult.success("数据获取成功。");
            }
        }
        catch(Exception e){
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        params.put("moduleTable","TP_LogisticsMmodeGrads_Ck");
        params.put("entity", new LogisticsMmodeGrads());
        params.put("toolBarWidth", 0);//操作
        params.put("checkBox", "yes");
        params.put("readOnly", false);//生成列是否可编辑
        params.put("getFieldAttributesType", "pageingrid");//pageingrid：网格字段// 区
        Map<String, Object> stringObjectMap = gridColumnsPropertyModel.queryGridColumnsPropertyModel(params);
        mv.addObject("enableEdit", 1); //当前页面是否可编辑（0：不可编辑，1：可编辑）
        mv.addObject("pageType","add");
        mv.addObject("gridColumnsJZ", stringObjectMap);
        mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
        mv.addObject("ajaxResult", ajaxResult);
        mv.setViewName(prefix + "/depot_add");
        return mv;
    }
    
    /**
     * 新增客户信息——数据提交
     * @param params
     * @return AjaxResult
     * @author lwl
     */
    @RequiresPermissions("system:depot:add")
    @Log(title = "新增仓库", businessType = BusinessType.INSERT)
    @PostMapping("/addpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult depotAddPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= depotInfoService.insertDepotInfo(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
	
    /**
     * 删除仓库信息
     * @param params
     * @return AjaxResult
     * @author lwl
     */
    @RequiresPermissions("system:depot:delete")
    @Log(title = "删除仓库", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult depotDelete(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= depotInfoService.deleteDepotInfo(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e)); 
        }
        return ajaxResult;
    }
    
    /**
     * 打开仓库信息修改页面
     * @param params
     * @return ModelAndView
     * @author lwl
     */
    @RequiresPermissions("system:depot:edit")
    @GetMapping("/edit")
    public ModelAndView depotEdit(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTable);
    	params.put("entity", new SysDepotInfo()); //用于获取实体属性名取代对应字典字段名，解决大小写敏感问题
    	ModelAndView mv = this.getModelAndView();
    	AjaxResult ajaxResult = new AjaxResult();
    	Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
    	SysDepotInfo depotInfo = new SysDepotInfo();
        mv = this.gotoModelAndViewErrorPage(params, "guid", "仓库编号");
        if (mv.isEmpty()){
            try{
                dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
                if (dictionaryForFieldMap.isEmpty()){
                    ajaxResult = AjaxResult.error("未获取到字段属性。");
                }else{
                	depotInfo = depotInfoService.queryDepotInfoByGuid(params.get("guid").toString());
                	if (depotInfo == null){
                		ajaxResult = AjaxResult.error("没有查到仓库[" + params.get("guid").toString() + "]。");
                	}else{
                		
                    	ajaxResult = AjaxResult.success("数据获取成功。");
                	}
                }
            }
            catch(Exception e){
                ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
            }
            params.put("moduleTable", "TP_LogisticsMmodeGrads_Ck");
            params.put("entity", new LogisticsMmodeGrads());
            params.put("toolBarWidth", 0);//操作
            params.put("checkBox", "yes");
            params.put("readOnly", false);//生成列是否可编辑
            params.put("getFieldAttributesType", "pageingrid");//pageingrid：网格字段// 区
            Map<String, Object> stringObjectMap = gridColumnsPropertyModel.queryGridColumnsPropertyModel(params);
            mv.addObject("pageType","edit");
            mv.addObject("enableEdit", 1); //当前页面是否可编辑（0：不可编辑，1：可编辑）
            mv.addObject("depotInfo",depotInfo);
            mv.addObject("gridColumnsJZ", stringObjectMap);
            mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
            mv.addObject("ajaxResult", ajaxResult);

            //获取物流名称详情
            Map<String, Object> params2 = new HashMap<String, Object>();
            params2.put("logisticsMmodeGuid",depotInfo.getGuid());
            //获取梯度
            List<LogisticsMmodeGrads> logisticsMmodeGradList = logsticsMmodeGradsMapper.findLogisticsMmodeGrads(params2);
            mv.addObject("logisticsMmodeGradList",logisticsMmodeGradList);
            mv.setViewName(prefix + "/depot_add");
        }
        return mv;
    }
    
    /**
     * 通过guid修改仓库信息--数据提交
     * @param params
     * @return AjaxResult
     * @author lwl
     */
    @RequiresPermissions("system:depot:edit")
    @Log(title = "修改仓库信息", businessType = BusinessType.UPDATE)
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult depotEditPost(@RequestParam Map<String, Object> params) {
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= depotInfoService.updateDepotInfoByGuid(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
}
